package a07_二叉树;

/**
 * <p>
 * a28_二叉搜索树的最近公共祖先复习1
 * </p>
 *
 * @author flyduck
 * @since 2024/12/25
 */
public class a28_二叉搜索树的最近公共祖先复习1 {
    //只需要判断root.val在p和q之间就行了
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }

        if(p.val > root.val && q.val > root.val){
            return lowestCommonAncestor(root.right, p, q);
        }else if(p.val < root.val && q.val < root.val){
            return lowestCommonAncestor(root.left, p, q);
        }else {
            return root;
        }
    }
}
